home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWMemory / Sources / FWMemorH.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  24.4 KB  |  854 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemorH.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWPLATME_H
  13. #include <FWPlatMe.h>
  14. #endif
  15.  
  16. #ifndef FWMEMORH_H
  17. #include <FWMemorH.h>
  18. #endif
  19.  
  20. #ifndef __MEMORY__
  21. #include <Memory.h>
  22. #endif
  23.  
  24. #ifndef __STDIO__
  25. #include <stdio.h>
  26. #endif
  27.  
  28.  
  29. //========================================================================================
  30. // CLASS FW_CMemoryHook (FW_DEBUG only)
  31. //========================================================================================
  32.  
  33. #ifdef FW_DEBUG
  34. //----------------------------------------------------------------------------------------
  35. // FW_CMemoryHook::FW_CMemoryHook
  36. //----------------------------------------------------------------------------------------
  37. #pragma segment HeapSeg
  38.  
  39. FW_CMemoryHook::FW_CMemoryHook()
  40. {
  41.     fNextHook = fPreviousHook = this;
  42. }
  43. #endif
  44.  
  45. #ifdef FW_DEBUG
  46. //----------------------------------------------------------------------------------------
  47. // FW_CMemoryHook::~FW_CMemoryHook
  48. //----------------------------------------------------------------------------------------
  49. #pragma segment HeapSeg
  50.  
  51. FW_CMemoryHook::~FW_CMemoryHook()
  52. {
  53. }
  54. #endif
  55.  
  56. #ifdef FW_DEBUG
  57. //----------------------------------------------------------------------------------------
  58. // FW_CMemoryHook::AboutToAllocate
  59. //----------------------------------------------------------------------------------------
  60. #pragma segment HeapSeg
  61.  
  62. FW_BlockSize FW_CMemoryHook::AboutToAllocate(FW_BlockSize size)
  63. {
  64.     return size;
  65. }
  66. #endif
  67.  
  68. #ifdef FW_DEBUG
  69. //----------------------------------------------------------------------------------------
  70. // FW_CMemoryHook::DidAllocate
  71. //----------------------------------------------------------------------------------------
  72. #pragma segment HeapSeg
  73.  
  74. void *FW_CMemoryHook::DidAllocate(void* blk, FW_BlockSize)
  75. {
  76.     return blk;
  77. }
  78. #endif
  79.  
  80. #ifdef FW_DEBUG
  81. //----------------------------------------------------------------------------------------
  82. // FW_CMemoryHook::AboutToFW_BlockSize
  83. //----------------------------------------------------------------------------------------
  84. #pragma segment HeapSeg
  85.  
  86. void *FW_CMemoryHook::AboutToBlockSize(void* blk)
  87. {
  88.     return blk;
  89. }
  90. #endif
  91.  
  92. #ifdef FW_DEBUG
  93. //----------------------------------------------------------------------------------------
  94. // FW_CMemoryHook::AboutToFree
  95. //----------------------------------------------------------------------------------------
  96. #pragma segment HeapSeg
  97.  
  98. void *FW_CMemoryHook::AboutToFree(void* blk)
  99. {
  100.     return blk;
  101. }
  102. #endif
  103.  
  104. #ifdef FW_DEBUG
  105. //----------------------------------------------------------------------------------------
  106. // FW_CMemoryHook::AboutToRealloc
  107. //----------------------------------------------------------------------------------------
  108. #pragma segment HeapSeg
  109.  
  110. void FW_CMemoryHook::AboutToRealloc(void* &, FW_BlockSize &)
  111. {
  112. }
  113. #endif
  114.  
  115. #ifdef FW_DEBUG
  116. //----------------------------------------------------------------------------------------
  117. // FW_CMemoryHook::DidRealloc
  118. //----------------------------------------------------------------------------------------
  119. #pragma segment HeapSeg
  120.  
  121. void *FW_CMemoryHook::DidRealloc(void *blk, FW_BlockSize)
  122. {
  123.     return blk;
  124. }
  125. #endif
  126.  
  127. #ifdef FW_DEBUG
  128. //----------------------------------------------------------------------------------------
  129. // FW_CMemoryHook::AboutToReset
  130. //----------------------------------------------------------------------------------------
  131. #pragma segment HeapSeg
  132.  
  133. void FW_CMemoryHook::AboutToReset()
  134. {
  135. }
  136. #endif
  137.  
  138. #ifdef FW_DEBUG
  139. //----------------------------------------------------------------------------------------
  140. // FW_CMemoryHook::Comment
  141. //----------------------------------------------------------------------------------------
  142. #pragma segment HeapSeg
  143.  
  144. void FW_CMemoryHook::Comment(const char*)
  145. {
  146. }
  147. #endif
  148.  
  149.  
  150. //========================================================================================
  151. // CLASS FW_CMemoryHookList (FW_DEBUG only)
  152. //========================================================================================
  153.  
  154. #ifdef FW_DEBUG
  155. //----------------------------------------------------------------------------------------
  156. // FW_CMemoryHookList::FW_CMemoryHookList
  157. //----------------------------------------------------------------------------------------
  158. #pragma segment HeapSeg
  159.  
  160. FW_CMemoryHookList::FW_CMemoryHookList()
  161. {
  162.     fCurrentHook = &fHead;
  163. }
  164. #endif
  165.  
  166. #ifdef FW_DEBUG
  167. //----------------------------------------------------------------------------------------
  168. // FW_CMemoryHookList::Add
  169. //----------------------------------------------------------------------------------------
  170. #pragma segment HeapSeg
  171.  
  172. void FW_CMemoryHookList::Add(FW_CMemoryHook* aMemoryHook)
  173. {
  174.     // Add at the fEnd of the list by adding after the last hook in the list.
  175.     
  176.     FW_CMemoryHook* afterHook = fHead.fPreviousHook;
  177.     
  178.     aMemoryHook->fNextHook = afterHook->fNextHook;
  179.     afterHook->fNextHook->fPreviousHook = aMemoryHook;
  180.     aMemoryHook->fPreviousHook = afterHook;
  181.     afterHook->fNextHook = aMemoryHook;
  182. }
  183. #endif
  184.  
  185. #ifdef FW_DEBUG
  186. //----------------------------------------------------------------------------------------
  187. // FW_CMemoryHookList::Remove
  188. //----------------------------------------------------------------------------------------
  189. #pragma segment HeapSeg
  190.  
  191. void FW_CMemoryHookList::Remove(FW_CMemoryHook* aMemoryHook)
  192. {
  193.     aMemoryHook->fPreviousHook->fNextHook = aMemoryHook->fNextHook;
  194.     aMemoryHook->fNextHook->fPreviousHook = aMemoryHook->fPreviousHook;
  195. }
  196. #endif
  197.  
  198. #ifdef FW_DEBUG
  199. //----------------------------------------------------------------------------------------
  200. // FW_CMemoryHookList::First
  201. //----------------------------------------------------------------------------------------
  202. #pragma segment HeapSeg
  203.  
  204. FW_CMemoryHook* FW_CMemoryHookList::First()
  205. {
  206.     fCurrentHook = fHead.fNextHook;
  207.     return fCurrentHook != &fHead ? fCurrentHook : NULL;
  208. }
  209. #endif
  210.  
  211. #ifdef FW_DEBUG
  212. //----------------------------------------------------------------------------------------
  213. // FW_CMemoryHookList::Next
  214. //----------------------------------------------------------------------------------------
  215. #pragma segment HeapSeg
  216.  
  217. FW_CMemoryHook* FW_CMemoryHookList::Next()
  218. {
  219.     if (fCurrentHook != &fHead)
  220.         fCurrentHook = fCurrentHook->fNextHook;
  221.     return fCurrentHook != &fHead ? fCurrentHook : NULL;
  222. }
  223. #endif
  224.  
  225. #ifdef FW_DEBUG
  226. //----------------------------------------------------------------------------------------
  227. // FW_CMemoryHookList::Previous
  228. //----------------------------------------------------------------------------------------
  229. #pragma segment HeapSeg
  230.  
  231. FW_CMemoryHook* FW_CMemoryHookList::Previous()
  232. {
  233.     if (fCurrentHook != &fHead)
  234.         fCurrentHook = fCurrentHook->fPreviousHook;
  235.     return fCurrentHook != &fHead ? fCurrentHook : NULL;
  236. }
  237. #endif
  238.  
  239. #ifdef FW_DEBUG
  240. //----------------------------------------------------------------------------------------
  241. // FW_CMemoryHookList::Last
  242. //----------------------------------------------------------------------------------------
  243. #pragma segment HeapSeg
  244.  
  245. FW_CMemoryHook* FW_CMemoryHookList::Last()
  246. {
  247.     fCurrentHook = fHead.fPreviousHook;
  248.     return fCurrentHook != &fHead ? fCurrentHook : NULL;
  249. }
  250. #endif
  251.  
  252. #ifdef FW_DEBUG
  253. //----------------------------------------------------------------------------------------
  254. // FW_CMemoryHookList::~FW_CMemoryHookList
  255. //----------------------------------------------------------------------------------------
  256. #pragma segment HeapSeg
  257.  
  258. FW_CMemoryHookList::~FW_CMemoryHookList()
  259. {
  260. }
  261. #endif
  262.  
  263.  
  264. //========================================================================================
  265. // CLASS FW_CMemoryHeap
  266. //========================================================================================
  267.  
  268. #ifdef FW_DEBUG
  269. const char *FW_CMemoryHeap::kDefaultDescription = "FW_CMemoryHeap";
  270. #endif
  271.  
  272. FW_CMemoryHeap *FW_CMemoryHeap::fHeapList;                                // Don't initialize!
  273.  
  274. //----------------------------------------------------------------------------------------
  275. // FW_CMemoryHeap::GetFirstHeap
  276. //----------------------------------------------------------------------------------------
  277. #pragma segment HeapSeg
  278.  
  279. FW_CMemoryHeap *FW_CMemoryHeap::GetFirstHeap()
  280. {
  281.     return fHeapList;
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // FW_CMemoryHeap::Allocate
  286. //----------------------------------------------------------------------------------------
  287. #pragma segment HeapSeg
  288.  
  289. void *FW_CMemoryHeap::Allocate(FW_BlockSize size)
  290. {
  291.     FW_BlockSize allocatedSize;
  292.     
  293. #ifdef FW_DEBUG
  294.     size = this->CallAboutToAllocateHooks(size);
  295. #endif
  296.     
  297.     void *blk = this->DoAllocate(size, allocatedSize);
  298.     
  299.     if (blk != NULL)
  300.     {
  301.         if (fZapOnAllocate)
  302.         {
  303.             char *chrBlk = (char *) blk;
  304.             for (FW_BlockSize i = 0; i < allocatedSize; i++)
  305.                 *chrBlk++ = 0;
  306.         }
  307.             
  308.         fBytesAllocated += allocatedSize;
  309.         fNumberAllocatedBlocks++;
  310.     }
  311.  
  312. #ifdef FW_DEBUG
  313.     blk = this->CallDidAllocateHooks(blk, size);
  314. #endif
  315.  
  316.     return blk;
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // FW_CMemoryHeap::FW_BlockSize
  321. //----------------------------------------------------------------------------------------
  322. #pragma segment HeapSeg
  323.  
  324. FW_BlockSize FW_CMemoryHeap::BlockSize(const void *blk) const
  325. {
  326.     if (blk == NULL)
  327.         return 0;
  328.         
  329. #ifdef FW_DEBUG
  330.     if (fAutoValidation)
  331.         this->ValidateAndReport((void*)blk);
  332.         
  333.     blk = ((FW_CMemoryHeap *) this)->CallAboutToFW_BlockSizeHooks((void*)blk);
  334. #endif
  335.  
  336.     return this->DoBlockSize((void*)blk);
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. // FW_CMemoryHeap::BytesAllocated
  341. //----------------------------------------------------------------------------------------
  342. #pragma segment HeapSeg
  343.  
  344. unsigned long FW_CMemoryHeap::BytesAllocated() const
  345. {
  346.     return fBytesAllocated;
  347. }
  348.  
  349. //----------------------------------------------------------------------------------------
  350. // FW_CMemoryHeap::Free
  351. //----------------------------------------------------------------------------------------
  352. #pragma segment HeapSeg
  353.  
  354. void FW_CMemoryHeap::Free(void* blk)
  355. {
  356.     if (blk == NULL)
  357.         return;
  358.         
  359. #ifdef FW_DEBUG
  360.     if (fAutoValidation)
  361.         this->ValidateAndReport(blk);
  362.         
  363.     blk = this->CallAboutToFreeHooks(blk);
  364. #endif
  365.     
  366.     FW_BlockSize allocatedSize = (FW_BlockSize) this->DoBlockSize(blk);
  367.     
  368.     if (fZapOnFree)
  369.     {
  370.         char *chrBlk = (char *) blk;
  371.         for (FW_BlockSize i = 0; i < allocatedSize; i++)
  372.             *chrBlk++ = 0;
  373.     }
  374.  
  375.     this->DoFree(blk);
  376.  
  377.     fBytesAllocated -= allocatedSize;
  378.     fNumberAllocatedBlocks--;
  379. }
  380.  
  381. #ifdef FW_DEBUG
  382. //----------------------------------------------------------------------------------------
  383. // FW_CMemoryHeap::GetAutoValidation (FW_DEBUG only)
  384. //----------------------------------------------------------------------------------------
  385. #pragma segment HeapSeg
  386.  
  387. Boolean FW_CMemoryHeap::GetAutoValidation() const
  388. {
  389.     return fAutoValidation;
  390. }
  391. #endif
  392.  
  393. #ifdef FW_DEBUG
  394. //----------------------------------------------------------------------------------------
  395. // FW_CMemoryHeap::GetDescription (FW_DEBUG only)
  396. //----------------------------------------------------------------------------------------
  397. #pragma segment HeapSeg
  398.  
  399. const char *FW_CMemoryHeap::GetDescription() const
  400. {
  401.     return fDescription;
  402. }
  403. #endif
  404.  
  405. //----------------------------------------------------------------------------------------
  406. // FW_CMemoryHeap::GetNextHeap
  407. //----------------------------------------------------------------------------------------
  408. #pragma segment HeapSeg
  409.  
  410. FW_CMemoryHeap *FW_CMemoryHeap::GetNextHeap() const
  411. {
  412.     return fNextHeap;
  413. }
  414.  
  415. //----------------------------------------------------------------------------------------
  416. // FW_CMemoryHeap::GetZapOnAllocate
  417. //----------------------------------------------------------------------------------------
  418. #pragma segment HeapSeg
  419.  
  420. Boolean FW_CMemoryHeap::GetZapOnAllocate() const
  421. {
  422.     return fZapOnAllocate;
  423. }
  424.  
  425. //----------------------------------------------------------------------------------------
  426. // FW_CMemoryHeap::GetZapOnFree
  427. //----------------------------------------------------------------------------------------
  428. #pragma segment HeapSeg
  429.  
  430. Boolean FW_CMemoryHeap::GetZapOnFree() const
  431. {
  432.     return fZapOnFree;
  433. }
  434.  
  435. #ifdef FW_DEBUG
  436. //----------------------------------------------------------------------------------------
  437. // FW_CMemoryHeap::InstallHook (FW_DEBUG only)
  438. //----------------------------------------------------------------------------------------
  439. #pragma segment HeapSeg
  440.  
  441. void FW_CMemoryHeap::InstallHook(FW_CMemoryHook *memoryHook)
  442. {
  443.     fMemoryHookList.Add(memoryHook);
  444. }
  445. #endif
  446.  
  447. #ifdef FW_DEBUG
  448. //----------------------------------------------------------------------------------------
  449. // FW_CMemoryHeap::IsValidBlock (FW_DEBUG only)
  450. //----------------------------------------------------------------------------------------
  451. #pragma segment HeapSeg
  452.  
  453. Boolean FW_CMemoryHeap::IsValidBlock(void *blk) const
  454. {
  455.     if (blk == NULL)
  456.         return false;
  457.         
  458.     if (this->IsMyBlock(blk))
  459.         return DoIsValidBlock(blk);
  460.     else
  461.         return false;
  462. }
  463. #endif
  464.  
  465. //----------------------------------------------------------------------------------------
  466. // FW_CMemoryHeap::NumberAllocatedBlocks
  467. //----------------------------------------------------------------------------------------
  468. #pragma segment HeapSeg
  469.  
  470. unsigned long FW_CMemoryHeap::NumberAllocatedBlocks() const
  471. {
  472.     return fNumberAllocatedBlocks;
  473. }
  474.  
  475. //----------------------------------------------------------------------------------------
  476. // FW_CMemoryHeap::Reallocate
  477. //----------------------------------------------------------------------------------------
  478. #pragma segment HeapSeg
  479.  
  480. void *FW_CMemoryHeap::Reallocate(void *blk, FW_BlockSize newSize)
  481. {
  482.     if (blk == NULL)
  483.         return this->Allocate(newSize);
  484.         
  485.     FW_BlockSize allocatedSize;
  486.     FW_BlockSize oldBlkSize = (FW_BlockSize) this->DoBlockSize(blk);
  487.     
  488. #ifdef FW_DEBUG
  489.     this->CallAboutToReallocHooks(blk, newSize);
  490. #endif
  491.     
  492.     blk = this->DoReallocate(blk, newSize, allocatedSize);
  493.     
  494.     if (blk != NULL)
  495.         fBytesAllocated += allocatedSize - oldBlkSize;
  496.             
  497. #ifdef FW_DEBUG
  498.     blk = this->CallDidAllocateHooks(blk, newSize);
  499. #endif
  500.  
  501.     return blk;
  502. }
  503.         
  504. #ifdef FW_DEBUG
  505. //----------------------------------------------------------------------------------------
  506. // FW_CMemoryHeap::RemoveHook (FW_DEBUG only)
  507. //----------------------------------------------------------------------------------------
  508. #pragma segment HeapSeg
  509.  
  510. void FW_CMemoryHeap::RemoveHook(FW_CMemoryHook *memoryHook)
  511. {
  512.     fMemoryHookList.Remove(memoryHook);
  513. }
  514. #endif
  515.  
  516. //----------------------------------------------------------------------------------------
  517. // FW_CMemoryHeap::Reset
  518. //----------------------------------------------------------------------------------------
  519. #pragma segment HeapSeg
  520.  
  521. void FW_CMemoryHeap::Reset()
  522. {
  523. #ifdef FW_DEBUG
  524.     this->CallAboutToResetHooks();
  525. #endif
  526.     
  527.     fBytesAllocated = 0;
  528.     fNumberAllocatedBlocks = 0;
  529.     
  530.     this->DoReset();
  531. }
  532.  
  533. #ifdef FW_DEBUG
  534. //----------------------------------------------------------------------------------------
  535. // FW_CMemoryHeap::SetAutoValidation (FW_DEBUG only)
  536. //----------------------------------------------------------------------------------------
  537. #pragma segment HeapSeg
  538.  
  539. void FW_CMemoryHeap::SetAutoValidation(Boolean autoValidation)
  540. {
  541.     fAutoValidation = autoValidation;
  542. }
  543. #endif
  544.  
  545. #ifdef FW_DEBUG
  546. //----------------------------------------------------------------------------------------
  547. // FW_CMemoryHeap::SetDescription (FW_DEBUG only)
  548. //----------------------------------------------------------------------------------------
  549. #pragma segment HeapSeg
  550.  
  551. void FW_CMemoryHeap::SetDescription(const char *description)
  552. {
  553.     // ------ Set the description without depending on other code
  554.     
  555.     const char *src = description;
  556.     char *dst = fDescription;
  557.     int i;
  558.     
  559.     for (i = 0; i < kDescriptionLength - 1 && *src; i++)
  560.         *dst++ = *src++;
  561.     fDescription[i] = 0;
  562. }
  563. #endif
  564.  
  565. //----------------------------------------------------------------------------------------
  566. // FW_CMemoryHeap::SetZapOnAllocate
  567. //----------------------------------------------------------------------------------------
  568. #pragma segment HeapSeg
  569.  
  570. void FW_CMemoryHeap::SetZapOnAllocate(Boolean zapOnAllocate)
  571. {
  572.     fZapOnAllocate = zapOnAllocate;
  573. }
  574.  
  575. //----------------------------------------------------------------------------------------
  576. // FW_CMemoryHeap::SetZapOnFree
  577. //----------------------------------------------------------------------------------------
  578. #pragma segment HeapSeg
  579.  
  580. void FW_CMemoryHeap::SetZapOnFree(Boolean zapOnFree)
  581. {
  582.     fZapOnFree = zapOnFree;
  583. }
  584.  
  585. //----------------------------------------------------------------------------------------
  586. // FW_CMemoryHeap::~FW_CMemoryHeap
  587. //----------------------------------------------------------------------------------------
  588. #pragma segment HeapSeg
  589.  
  590. FW_CMemoryHeap::~FW_CMemoryHeap()
  591. {
  592.     // Remove from the static list of heaps
  593.     
  594.     FW_CMemoryHeap *lastHeap = NULL, *currentHeap = fHeapList;
  595.     
  596.     while (currentHeap != NULL)
  597.     {
  598.         if (this == currentHeap)
  599.         {
  600.             if (lastHeap == NULL)
  601.                 fHeapList = currentHeap->GetNextHeap();
  602.             else
  603.                 lastHeap->fNextHeap = currentHeap->GetNextHeap();
  604.             
  605.             currentHeap = NULL;
  606.         }
  607.         else
  608.         {
  609.             lastHeap = currentHeap;
  610.             currentHeap = currentHeap->GetNextHeap();
  611.         }
  612.     }
  613. }
  614.  
  615. //----------------------------------------------------------------------------------------
  616. // FW_CMemoryHeap::FW_CMemoryHeap
  617. //----------------------------------------------------------------------------------------
  618. #pragma segment HeapSeg
  619.  
  620. FW_CMemoryHeap::FW_CMemoryHeap(Boolean autoValidation,
  621.                        Boolean zapOnAllocate,
  622.                        Boolean zapOnFree)
  623. {
  624. #ifdef FW_DEBUG
  625.     // ------ Set the default description without depending on other code
  626.     
  627.     const char *src = kDefaultDescription;
  628.     char *dst = fDescription;    
  629.     for (; *src;)
  630.         *dst++ = *src++;
  631.     *dst = 0;
  632. #endif
  633.  
  634.     fAutoValidation = autoValidation;
  635.     fZapOnAllocate = zapOnAllocate;
  636.     fZapOnFree = zapOnFree;
  637.     fBytesAllocated = 0;
  638.     fNumberAllocatedBlocks = 0;
  639.     
  640.     // Add to the static list of heaps
  641.     
  642.     fNextHeap = fHeapList;
  643.     fHeapList = this;
  644. }
  645.  
  646. //----------------------------------------------------------------------------------------
  647. // FW_CMemoryHeap::operator new
  648. //----------------------------------------------------------------------------------------
  649. #pragma segment HeapSeg
  650.  
  651. void* FW_CMemoryHeap::operator new(SIZE_T size)
  652. {
  653.     return PlatformAllocateBlock(size);
  654. }
  655.  
  656. //----------------------------------------------------------------------------------------
  657. // FW_CMemoryHeap::operator delete
  658. //----------------------------------------------------------------------------------------
  659. #pragma segment HeapSeg
  660.  
  661. void FW_CMemoryHeap::operator delete(void* ptr)
  662. {
  663.     PlatformFreeBlock(ptr);
  664. }
  665.  
  666. //----------------------------------------------------------------------------------------
  667. // FW_CMemoryHeap::AllocateRawMemory
  668. //----------------------------------------------------------------------------------------
  669. #pragma segment HeapSeg
  670.  
  671. void *FW_CMemoryHeap::AllocateRawMemory(FW_BlockSize size)
  672. {
  673.     return PlatformAllocateBlock(size);
  674. }
  675.  
  676. //----------------------------------------------------------------------------------------
  677. // FW_CMemoryHeap::DoReallocate
  678. //----------------------------------------------------------------------------------------
  679. #pragma segment HeapSeg
  680.  
  681. void *FW_CMemoryHeap::DoReallocate(void *block, FW_BlockSize newSize, FW_BlockSize &allocatedSize)
  682. {
  683.     FW_BlockSize oldRealSize = this->DoBlockSize(block);
  684.     void* newBlock = this->DoAllocate(newSize, allocatedSize);
  685.     
  686.     if (newBlock != NULL)
  687.     {
  688.         FW_BlockSize copySize = newSize <= oldRealSize ? newSize : oldRealSize;
  689.         PlatformCopyMemory(block, newBlock, copySize);
  690.  
  691.         this->DoFree(block);
  692.     }
  693.     else
  694.       if (newSize <= oldRealSize)   //if unable to get new fMem, and newSize is <= real size
  695.         return block;               //then return original ptr unchanged
  696.  
  697.     return newBlock;
  698. }
  699.         
  700. //----------------------------------------------------------------------------------------
  701. // FW_CMemoryHeap::FreeRawMemory
  702. //----------------------------------------------------------------------------------------
  703. #pragma segment HeapSeg
  704.  
  705. void FW_CMemoryHeap::FreeRawMemory(void* ptr)
  706. {
  707.     PlatformFreeBlock(ptr);
  708. }
  709.  
  710. #ifdef FW_DEBUG
  711. //----------------------------------------------------------------------------------------
  712. // FW_CMemoryHeap::CompilerCheck (FW_DEBUG only)
  713. //----------------------------------------------------------------------------------------
  714. #pragma segment HeapSeg
  715.  
  716. void FW_CMemoryHeap::CompilerCheck()
  717. {
  718. }
  719. #endif
  720.  
  721. #ifdef FW_DEBUG
  722. //----------------------------------------------------------------------------------------
  723. // FW_CMemoryHeap::CallAboutToAllocateHooks (FW_DEBUG only)
  724. //----------------------------------------------------------------------------------------
  725. #pragma segment HeapSeg
  726.  
  727. FW_BlockSize FW_CMemoryHeap::CallAboutToAllocateHooks(FW_BlockSize size)
  728. {
  729.     for (FW_CMemoryHook *hook = fMemoryHookList.First();
  730.             hook != NULL; hook = fMemoryHookList.Next())
  731.         size = hook->AboutToAllocate(size);
  732.         
  733.     return size;
  734. }
  735. #endif
  736.  
  737. #ifdef FW_DEBUG
  738. //----------------------------------------------------------------------------------------
  739. // FW_CMemoryHeap::CallDidAllocateHooks (FW_DEBUG only)
  740. //----------------------------------------------------------------------------------------
  741. #pragma segment HeapSeg
  742.  
  743. void *FW_CMemoryHeap::CallDidAllocateHooks(void* blk, FW_BlockSize size)
  744. {
  745.     for (FW_CMemoryHook *hook = fMemoryHookList.First();
  746.             hook != NULL; hook = fMemoryHookList.Next())
  747.         blk = hook->DidAllocate(blk, size);
  748.         
  749.     return blk;
  750. }
  751. #endif
  752.  
  753. #ifdef FW_DEBUG
  754. //----------------------------------------------------------------------------------------
  755. // FW_CMemoryHeap::CallAboutToFW_BlockSizeHooks (FW_DEBUG only)
  756. //----------------------------------------------------------------------------------------
  757. #pragma segment HeapSeg
  758.  
  759. void *FW_CMemoryHeap::CallAboutToFW_BlockSizeHooks(void* blk)
  760. {
  761.     for (FW_CMemoryHook *hook = fMemoryHookList.Last();
  762.             hook != NULL; hook = fMemoryHookList.Previous())
  763.         blk = hook->AboutToBlockSize(blk);
  764.         
  765.     return blk;
  766. }
  767. #endif
  768.  
  769. #ifdef FW_DEBUG
  770. //----------------------------------------------------------------------------------------
  771. // FW_CMemoryHeap::CallAboutToFreeHooks (FW_DEBUG only)
  772. //----------------------------------------------------------------------------------------
  773. #pragma segment HeapSeg
  774.  
  775. void *FW_CMemoryHeap::CallAboutToFreeHooks(void* blk)
  776. {
  777.     for (FW_CMemoryHook *hook = fMemoryHookList.Last();
  778.             hook != NULL; hook = fMemoryHookList.Previous())
  779.         blk = hook->AboutToFree(blk);
  780.         
  781.     return blk;
  782. }
  783. #endif
  784.  
  785. #ifdef FW_DEBUG
  786. //----------------------------------------------------------------------------------------
  787. // FW_CMemoryHeap::CallAboutToReallocHooks (FW_DEBUG only)
  788. //----------------------------------------------------------------------------------------
  789. #pragma segment HeapSeg
  790.  
  791. void FW_CMemoryHeap::CallAboutToReallocHooks(void* &blk, FW_BlockSize& size)
  792. {
  793.     for (FW_CMemoryHook *hook = fMemoryHookList.Last();
  794.             hook != NULL; hook = fMemoryHookList.Previous())
  795.         hook->AboutToRealloc(blk, size);
  796. }
  797. #endif
  798.  
  799. #ifdef FW_DEBUG
  800. //----------------------------------------------------------------------------------------
  801. // FW_CMemoryHeap::CallDidReallocHooks (FW_DEBUG only)
  802. //----------------------------------------------------------------------------------------
  803. #pragma segment HeapSeg
  804.  
  805. void *FW_CMemoryHeap::CallDidReallocHooks(void *blk, FW_BlockSize size)
  806. {
  807.     for (FW_CMemoryHook *hook = fMemoryHookList.First();
  808.             hook != NULL; hook = fMemoryHookList.Next())
  809.         blk = hook->DidRealloc(blk, size);
  810.  
  811.     return blk;
  812. }
  813. #endif
  814.  
  815. #ifdef FW_DEBUG
  816. //----------------------------------------------------------------------------------------
  817. // FW_CMemoryHeap::CallAboutToResetHooks (FW_DEBUG only)
  818. //----------------------------------------------------------------------------------------
  819. #pragma segment HeapSeg
  820.  
  821. void FW_CMemoryHeap::CallAboutToResetHooks()
  822. {
  823.     for (FW_CMemoryHook *hook = fMemoryHookList.First();
  824.             hook != NULL; hook = fMemoryHookList.Next())
  825.         hook->AboutToReset();
  826. }
  827. #endif
  828.  
  829. #ifdef FW_DEBUG
  830. //----------------------------------------------------------------------------------------
  831. // FW_CMemoryHeap::CallCommentHooks (FW_DEBUG only)
  832. //----------------------------------------------------------------------------------------
  833. #pragma segment HeapSeg
  834.  
  835. void FW_CMemoryHeap::CallCommentHooks(const char* comment)
  836. {
  837.     for (FW_CMemoryHook *hook = fMemoryHookList.First();
  838.             hook != NULL; hook = fMemoryHookList.Next())
  839.         hook->Comment(comment);
  840. }
  841. #endif
  842.  
  843. #ifdef FW_DEBUG
  844. //----------------------------------------------------------------------------------------
  845. // FW_CMemoryHeap::ValidateAndReport (FW_DEBUG only)
  846. //----------------------------------------------------------------------------------------
  847. #pragma segment HeapSeg
  848.  
  849. void FW_CMemoryHeap::ValidateAndReport(void *blk) const
  850. {
  851.     PLATFORM_ASSERT(this->IsValidBlock(blk));
  852. }
  853. #endif
  854.